package com.stars.empoly.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.google.gson.Gson
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.base.MBActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.ActivityIssueBinding
import com.stars.empoly.inte.ChoiceListData
import com.stars.empoly.inte.ChoseTimeData
import com.stars.empoly.model.SelectData
import com.stars.empoly.model.User
import com.stars.empoly.model.bean.ChoseCityDatas
import com.stars.empoly.model.bean.ChoseMapAddress
import com.stars.empoly.utils.ChoseLocationTime
import com.stars.empoly.utils.PopuBottomUtils
import com.stars.empoly.utils.PopuCarTypeUtils
import com.stars.empoly.vmodel.IssueVModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch

/**
 *on 2021/5/7
 */
class IssueActivity: MBActivity<IssueVModel, ActivityIssueBinding>() {

    var choseCityDatas: ChoseCityDatas?=null
    //选择出发地目的地
    var type=1//1出发地 2目的地
    var order_id:String?=""

    override fun __before() {
        super.__before()
        var item = intent?.extras?.getString("item")
        choseCityDatas = Gson().fromJson(item, ChoseCityDatas::class.java)

         order_id=intent?.extras?.getString("order_id")

    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        if(TextUtils.isEmpty(order_id)) {
            if (null != choseCityDatas) {
                vm.item.value = choseCityDatas
            }

            //默认发车人

        }else{
            //获取订单详情
            vm.getInfo(order_id)

        }



        vm.getCarType()

        vm.carTypeList.observe(this, Observer {
            if(vm.isScooter){

                PopuCarTypeUtils.show(this,it, binding.llTypeCart, object : ChoiceListData {
                    override fun onRestuse(type_id: String?,name:String?) {
                        //todo
                        vm.type_id=type_id
                        vm.typeIdName.value=name

                    }
                })
            }
        })
        vm.placeOrder.observe(this, Observer {
            //下完单后的操作
//            if(vm.payment.equals("0")){
//                //全款
//
//                UiSwitch.bundleRes(this, PayActivity::class.java,
//                        B().putString("order_id", it.order_id)
//                                .putString("price",it.amount)
//                                .ok(),MIntentAction.REQUEST_CODE_TWO)
//
//
//                return@Observer
//            }
            if(vm.payment.equals("1")){
                //到付

                UiSwitch.bundle(this,WayDetailsActivity::class.java,
                    B().putString("order_id",it.order_id)
                            .ok())
                return@Observer
            }

//            if(vm.payment.equals("2")){
//                //付定金
//                UiSwitch.bundleRes(this, PayActivity::class.java,
//                        B().putString("order_id", it.order_id)
//                                .putString("price",it.amount)
//                                .ok(),MIntentAction.REQUEST_CODE_TWO)
//
//                return@Observer
//            }
        })

        vm.bean.observe(this, Observer {
            if(null==it){
                return@Observer
            }

            //todo：缺少  起始城市名
            var item=ChoseCityDatas(it.start_position,it.start_address,"",it.start_lat.toDouble(),it.start_lng.toDouble(),
                    it.end_position,it.end_address,"",it.end_lat.toDouble(),it.end_lng.toDouble()
                    )

            vm.item.value = item
            //用车时间
            vm.start_driving_time=it.start_driving_time
            vm.end_driving_time=it.end_driving_time
            vm.timeOne.value= it.start_driving_time+"～"+it.end_driving_time
            //车辆型号 todo：缺少
//            vm.vehicleModel=it.vehicle_model
//            vm.vehicle_status=it.vehicle_status
//            if(it.vehicle_status=="0"){
//                vm.vehicletatusName.value="正常"
//            }else{
//                vm.vehicletatusName.value="故障车"
//            }


            //发车人
            vm.consignorName = it.consignor_name
            vm.phone = it.consignor_mobile
            //接车人
            vm.receivingName=it.receiving_name
            vm.mobile=it.receiving_mobile
            //板车类型 todo：缺少
//            vm.type_id=it.type_id
//            vm.typeIdName.value=it.name
            //备注
            vm.remarks=it.remarks
            //付款方式

            if(vm.payment.equals("0")){//全款
               vm.payment="0"
                vm.choseOne.value = true
                vm.choseTwo.value = false
                vm.choseThree.value = false
                vm.money=it.order_amount
                vm.amount.value=it.order_amount

            }
            if(vm.payment.equals("2")){//付定金
                vm.payment="2"
                vm.choseOne.value = false
                vm.choseTwo.value = false
                vm.choseThree.value = true
                vm.money=it.order_amount
                //todo：缺少 平台预估金额
                vm.amount.value=it.order_amount
            }




        })



    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_ONE === requestCode && resultCode == Activity.RESULT_OK) {
            //终点信息
            val bundle = data!!.extras
            if (null != bundle) {

                var item = bundle.getString("item")
                val choseMapAddress: ChoseMapAddress = Gson().fromJson(item, ChoseMapAddress::class.java)

                if(type==1){


                    vm.item.value?.startCity = choseMapAddress.cityname
                    vm.item.value?.startLat = choseMapAddress.latitude.toDouble()
                    vm.item.value?.startLng = choseMapAddress.longitude.toDouble()
                    vm.item.value?.startDetailsPosition = choseMapAddress.address
                    vm.item.value?.startPosition = choseMapAddress.position
                    var ss = vm.item.value
                    vm.item.value = ss

                }else {
                    vm.item.value?.endCity = choseMapAddress.cityname
                    vm.item.value?.endLat = choseMapAddress.latitude.toDouble()
                    vm.item.value?.endLng = choseMapAddress.longitude.toDouble()
                    vm.item.value?.endDetailsPosition = choseMapAddress.address
                    vm.item.value?.endPosition = choseMapAddress.position
                    var ss = vm.item.value
                    vm.item.value = ss
                }

            }



            return
        }

        if (MIntentAction.REQUEST_CODE_TWO === requestCode && resultCode == Activity.RESULT_OK) {
            //付款完成

            val bundle = data!!.extras
            if (null != bundle) {
                var order_id = bundle.getString("order_id")!!
                UiSwitch.bundle(this, WayDetailsActivity::class.java,
                        B().putString("order_id", order_id)
                                .ok())

                finish()
            }

            return
        }


        }

    override fun listeners() {
        super.listeners()


        binding.tvStartPlace.onClick {

            type=1
            UiSwitch.bundleRes(this, ChosePlaceActivity::class.java,
                    B.with().putString("city", vm.item.value?.startCity).putString("address", vm.item.value?.startPosition).ok(),
                    MIntentAction.REQUEST_CODE_ONE)
        }
        binding.tvEndPlace.onClick {

            type=2
            UiSwitch.bundleRes(this, ChosePlaceActivity::class.java,
                    B.with().putString("city", vm.item.value?.endCity).putString("address", vm.item.value?.endPosition).ok(),
                    MIntentAction.REQUEST_CODE_ONE)
        }

        binding.tvStartAddress.onClick {

            type=1
            UiSwitch.bundleRes(this, ChosePlaceActivity::class.java,
                    B.with().putString("city", vm.item.value?.startCity).putString("address", vm.item.value?.startPosition).ok(),
                    MIntentAction.REQUEST_CODE_ONE)
        }
        binding.tvEndAddress.onClick {

            type=2
            UiSwitch.bundleRes(this, ChosePlaceActivity::class.java,
                    B.with().putString("city", vm.item.value?.endCity).putString("address", vm.item.value?.endPosition).ok(),
                    MIntentAction.REQUEST_CODE_ONE)
        }



        binding.llChoseTime.onClick {
            //选择时间

            ChoseLocationTime.show(context(),vm.start_driving_time,vm.end_driving_time,binding.llChoseTime,object : ChoseTimeData {
                override fun onRestuse(day: String?, dayTo: String?) {
                    vm.start_driving_time = day
                    vm.end_driving_time = dayTo
                    vm.timeOne.value= "$day～$dayTo"

                }
            })

        }




        binding.llTypeCart.onClick {
            //板车类型

            if(null==vm.carTypeList.value){
                vm.isScooter=true
                vm.getCarType()

            }else {
                PopuCarTypeUtils.show(this, vm.carTypeList.value!!,binding.llTypeCart, object : ChoiceListData {
                    override fun onRestuse(type_id: String?,name:String?) {
                        vm.type_id=type_id
                        vm.typeIdName.value=name

                        vm.setSumFee()

                    }
                })
            }

        }
        binding.butCatReason.onClick {
            //车的原因

            var list= ArrayList<SelectData>()
            var one=SelectData("0","正常")
            var two=SelectData("1","故障车")
            list.add(one)
            list.add(two)
            PopuBottomUtils.showList(this,list, object :ChoiceListData{
                override fun onRestuse(code: String?, name: String?) {

                    vm.vehicle_status=code
                    vm.vehicletatusName.value=name

                }
            })




        }
        binding.llPush.onClick {
            //发布

            vm.toPushSource()


        }



    }

    override fun vmClass(): Class<IssueVModel> {
        return IssueVModel::class.java
    }
    override fun binding(): ActivityIssueBinding {
        return ActivityIssueBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "发布货源"
    }
}
